import axios from "axios";

//  此处创建的axios的实力 命名不能是axios 否则和导入对象冲突
const instance = axios.create({
    baseURL: "http://localhost:8081/mdi/v1",
    timeout: 1000,
    headers: {
        "Content-Type": "application/json",
    },
});

// 请求拦截器
instance.interceptors.request.use(
    (config) => {
        // 定义不需要添加 Authorization 头的路径
        const exemptPaths = ['/user/login', '/user/register', '/user/logout'];

        // 检查请求路径是否在豁免列表中
        if (!exemptPaths.includes(config.url)) {
            const token = localStorage.getItem('token');
            if (token) {
                config.headers.Authorization = `Bearer ${token}`;
            }
        }
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);


// 暴露出去
export default instance;